package com.runa.monitor.platinterface.netservice.equipment;

import com.alibaba.fastjson.JSON;
import com.runa.monitor.comm.dto.Archive;
import com.runa.monitor.comm.dto.Node;
import com.runa.monitor.platinterface.backwork.function.TaskFunction;
import com.runa.monitor.platinterface.netservice.connect.commstatus.CommStatus;
import com.runa.persistence.task.TaskTypeEnum;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.analysis.huizhong.HuiZhongOneAnalysis;
import com.runa.rccprotocol.dto.HuiZhongOne.HuiZhongOneRequestData;
import com.runa.rccprotocol.dto.equdata.EquData;
import com.runa.rccprotocol.dto.param.ExtendParam;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Slf4j
public class HuiZhongOneDriver extends NetEqu<String> {

    public HuiZhongOneDriver(String serial, Integer timeout) {
        super(serial, timeout);
    }

    @Override
    public void readNodeEqu(List<Node> nodeList, TaskFunction function, EquData.EquDataType type) {
        if (!online()) {
            function.doApply(CommStatus.OffLine, nodeList);
            return;
        }
        switch (type) {
            case METER:
                readHouseMeter(nodeList, function);
                break;
            case PUBLIC_UNIT:
            case VALVE:
                readHouseValve(nodeList, function);
                break;
            default:
        }
    }

    private void readHouseMeter(List<Node> hMeters, TaskFunction function) {
        for (Node node : hMeters) {
            try {
                HuiZhongOneRequestData requestData = new HuiZhongOneRequestData(serial());
                requestData.readHeatMeter(node.getIndex());
                log.debug("{} 抄表：{}", serial(), ByteUtil.byteToHexString(requestData.getData()));
                requestData.setRemain(Collections.singletonList(node));
                sendRequest(requestData, function, TaskTypeEnum.READ_METER);
            } catch (Exception e) {
                log.error("{} HuiZhongOneDriver.readHouseMeter: {}", serial(), e);
            }
        }
    }

    private void readHouseValve(List<Node> valves, TaskFunction function) {
        for (Node valve : valves) {
            try {
                HuiZhongOneRequestData requestData = new HuiZhongOneRequestData(serial());
                requestData.readHouseValve(valve.getIndex()-100);
                log.debug("{} 抄阀：{}", serial(), ByteUtil.byteToHexString(requestData.getData()));
                requestData.setRemain(Collections.singletonList(valve));
                sendRequest(requestData, function, TaskTypeEnum.READ_VALVE);
            } catch (Exception e) {
                log.error("{} HuiZhongOneDriver.readHouseValve: {}", serial(), e);
            }
        }
    }

    @Override
    public void valveOnOffControl(List<Node> nodeList, boolean ifLock, Integer opening, TaskFunction function) {
        Integer open = opening == 0 ? 1 : 0;
        for (Node node : nodeList) {
            HuiZhongOneRequestData requestData = new HuiZhongOneRequestData(serial());
            requestData.valveControl(node.getIndex()-100, open);
            log.debug("{} 控阀：{}", serial(), ByteUtil.byteToHexString(requestData.getData()));
            requestData.setRemain(node);
            sendRequest(requestData, function);
        }
    }


    public static void main(String[] args) {
        HuiZhongOneRequestData requestData = new HuiZhongOneRequestData("0150210622");
        requestData.readHeatMeter(36);
        String s = ByteUtil.byteToHexString(requestData.getData());
        System.out.println(s);
    }

    @Override
    public void setNodeEquArchive(List<Archive> archives, TaskFunction function) {

    }

    @Override
    public void clearNodeEquArchive(TaskFunction function) {

    }

    @Override
    public void readNodeRecord(List<Integer> indexes, TaskFunction function) {

    }

    @Override
    public void sentPublicUnitRecord(List<Node> valveSerial, Integer model, Map param, TaskFunction function) {

    }

    @Override
    public void sentPublicUnitModel(List<Node> valveSerial, boolean model, TaskFunction function) {

    }

    @Override
    public void sentExtendParam(ExtendParam param, TaskFunction function) {

    }

    @Override
    public void readPublicUnitRecord(Node valveSerial, Integer model, TaskFunction function) {

    }

    @Override
    public void readPublicUnitAndMeter(Node valveSerial, Node meterSerial, TaskFunction function) {

    }

    @Override
    public void heartbeatUpdate() {

    }

    @Override
    public boolean heartStop() {
        return false;
    }
}
